package com.caizi.controller;

import com.caizi.mapper.MapDataMapper;
import com.caizi.pojo.MapData;
import com.caizi.service.MapDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mapData")
public class MapDataController {
    @Autowired
    private MapDataService mapDataService;

    @Autowired
    private MapDataMapper mapDataMapper;

    // 根据ID查询
    @GetMapping("/{id}")
    public MapData getById(@PathVariable Long id) {
        return mapDataService.getById(id);
    }

    // 根据Key查询
    @GetMapping("/key/{key}")
    public List<MapData> getByKey(@PathVariable String key) {
        return mapDataService.listByKeyContaining(key);
    }

    @GetMapping("/key/")
    public List<MapData> getAll() {
        return mapDataMapper.selectList(null);
    }

    // 组合条件查询（type+subType）
    @GetMapping("/type")
    public List<MapData> getByType(@RequestParam String type,
                                   @RequestParam(required = false) String subType) {
        return mapDataService.getByType(type, subType);
    }

    // 新增数据
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Boolean create(@RequestBody MapData mapData) {
        return mapDataService.create(mapData);
    }

    // 根据ID更新
    @PutMapping("/{id}")
    public Boolean updateById(@PathVariable Long id, @RequestBody MapData mapData) {
        mapData.setId(id);
        return mapDataService.updateById(mapData);
    }

    // 根据Key更新
    @PutMapping("/key/{key}")
    public Boolean updateByKey(@PathVariable String key, @RequestBody MapData mapData) {
        mapData.setKey(key); // 确保key一致性
        return mapDataService.updateByKey(mapData);
    }

    // 根据ID删除
    @DeleteMapping("/{id}")
    public Boolean deleteById(@PathVariable Long id) {
        return mapDataService.deleteById(id);
    }

    // 根据Key删除
    @DeleteMapping("/key/{key}")
    public Boolean deleteByKey(@PathVariable String key) {
        return mapDataService.deleteByKey(key);
    }
}